package com.kikoulol.jarmoryextactor.utils;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.SocketAddress;
import java.net.URL;
import java.net.URLConnection;

public class Utils {
	
	public static int connectionTry = 0;

	public static String convertStreamToString(InputStream is) throws IOException {
		/*
		 * To convert the InputStream to String we use the BufferedReader.readLine()
		 * method. We iterate until the BufferedReader return null which means
		 * there's no more data to read. Each line will appended to a StringBuilder
		 * and returned as String.
		 */
		if (is != null) {
			StringBuilder sb = new StringBuilder();
			String line;

			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
				while ((line = reader.readLine()) != null) {
					sb.append(line).append("\n");
				}
			} finally {
				is.close();
			}
			return sb.toString();
		} else {       
			return "";
		}
	}

	public static void downloadFile(String url, String to) {

		DataInputStream in=null;
		DataOutputStream out=null;
		FileOutputStream fOut=null;

		try{
			URL remoteFile=new URL(url);
			URLConnection fileStream=remoteFile.openConnection();
			fileStream.setRequestProperty( "User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1" );

			// Open the input streams for the remote file 
			fOut=new FileOutputStream(to);

			// Open the output streams for saving this file on disk
			out=new DataOutputStream(fOut);

			in=new DataInputStream(fileStream.getInputStream());

			// Read the remote on save save the file
			int data;
			while((data=in.read())!=-1){
				fOut.write(data);
			}  
			System.out.println("Download of " + url + " is complete." );   
		} catch (Exception e){
			e.printStackTrace();
		} finally {
			try{
				in.close();
				fOut.flush(); 
				fOut.close();      
			} catch(Exception e){e.printStackTrace();}

		}
	}

	public static String getArmoryXml(String armoryUrl) {
		System.out.println("getting : " + armoryUrl);
		String nextLine;
		URL url = null;
		URLConnection urlConn = null;
		InputStreamReader  inStream = null;
		BufferedReader buff = null;
		try{
			// Create the URL obect that points
			// at the default file index.html
			url  = new URL(armoryUrl);
			/*
			SocketAddress addr = new InetSocketAddress("localhost", 1081);
			Proxy proxy = new Proxy(Proxy.Type.SOCKS,addr);
			*/
			//System.out.println("debut d'attente avant connection");
			Wait.manySec(2);
			connectionTry++;
			//System.out.println(connectionTry + " connection" + "(" + armoryUrl + ")");
			//System.out.println("Fin d'attente, Tentative de connection");
			urlConn = url.openConnection(/*proxy*/);
			urlConn.setRequestProperty( "User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1" );
			inStream = new InputStreamReader( 
					urlConn.getInputStream());
			//System.out.println("commence a pomper le site");
			buff= new BufferedReader(inStream);

			StringBuffer reader = new StringBuffer();

			// Read and print the lines from index.html
			while (true){
				nextLine =buff.readLine();  
				if (nextLine !=null){
					//System.out.println(nextLine);
					reader.append(nextLine);
				}
				else{
					break;
				} 
			}
			return reader.toString();
		} catch(MalformedURLException e){
			System.out.println("Please check the URL:" + e.toString() );
		} catch(IOException  e1){
			System.out.println("Can't read  from the Internet: "+	e1.toString() );
		}
		return null;
	}
}
